// pages/chargeMoney/chargeMoney.js
var app = getApp()
const ApiRootUrl = 'http://8.136.195.245:8000/';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scanStr:"",
    androidDialog2: false,
    resmsg:"",
    cardUid:"",
    buyWay: 0,
    payWay: 0,
    fruitName: "",
    fruitWeight: 0,
    unitPrice: 0,
    totalPrice:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 扫码支付，获取oled上二维码包含的水果名、重量、单价信息、总价信息
  getScancode() {
    var that = this
    wx.scanCode({
      success(res) {
        console.log("res.result=", res.result)
        var splitStr = res.result.split("+")
        console.log("fruitName=", splitStr[0])
        console.log("unitPrice=", splitStr[2])
        console.log("type=", typeof (splitStr[2]))
        that.setData({
          scanStr: res.result,
          fruitName:splitStr[0],
          fruitWeight:splitStr[1],
          unitPrice:splitStr[2],
          totalPrice:splitStr[3],
          androidDialog2: true
        })
      }
    })
  },
  //扫描屏幕上的二维码，生成订单记录
  scanPay: function () {
    let that = this
    console.log("你在进行api请求")
    console.log("userid:", app.globalData.userId)
    // console.log("carduid:", that.data.cardUid)
    // console.log("typeOfuserid:", typeof (app.globalData.userId))
    wx.request({
      // url: 'http://8.136.195.245:8000/card/bindUser',
      // url: 'http://localhost:8001/orders/payOrder',
      url:ApiRootUrl+'orders/payOrder',
      data: {
        userId: app.globalData.userId,
        buyWay: 2,
        payWay: 2,
        fruitName: that.data.fruitName,
        fruitWeight: that.data.fruitWeight,
        unitPrice: that.data.unitPrice,
        totalPrice:that.data.totalPrice
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'chartset': 'utf-8'
      },
      success(res) {
        console.log("获取数据成功", res)
        console.log("code:", res.data.code)
        that.setData({
          msg: res.data.message
        })
        console.log("res.data.message", res.data.message)
      }
    })
  },
  close: function () {
    this.setData({
      androidDialog2: false,
    })

  },
  confirm: function () {
    console.log("你点击了确定支付按钮")
    this.setData({
      androidDialog2: false,
    })
    this.scanPay()
  },

})